package com.huluobo.a2203parkdemo.signin

import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.adapter.SignAdapter
import com.huluobo.a2203parkdemo.databinding.ActivitySignInBinding
import com.huluobo.base.bean.LoginBean
import com.huluobo.base.bean.SignBeanList
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.text.SimpleDateFormat

@AndroidEntryPoint
class SignInActivity : BaseActivity<ActivitySignInBinding, SignInViewModel>() {

    private var signId = 0//签到签退id,0代表签到 1代表签退
    private var userId = 0//用户id
    private var time = ""
    private val address = "天津市"

    private lateinit var signAdapter: SignAdapter

    override fun initData() {
        viewModel.getUser(SPUtils.getInstance().getString(Const.PARAM_USERNAME))
//        val map = mutableMapOf<String, Int>()
//        map[Const.PARAM_MONTH] = 8
//        val json = Gson().toJson(map)
//        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
//        viewModel.getAllSign(body)

        viewModel.getAllSignSuccess.observe(this) {
            val list = Gson().fromJson(it, SignBeanList::class.java)
            signAdapter.data.clear()
            signAdapter.data.addAll(list)
            signAdapter.notifyDataSetChanged()
        }

        viewModel.getUserComplete.observe(this) {
            userId = it.uId
        }

        viewModel.signInSuccess.observe(this) {
            ToastUtils.showLong("签到成功")
            binding.signInBtn.text = "签退"
            signId = 1
            SPUtils.getInstance().put(Const.PARAM_SIGN_ID, signId)
        }

        viewModel.signOutSuccess.observe(this) {
            ToastUtils.showLong("签退成功")
            binding.signInBtn.text = "签到"
            signId = 0
            SPUtils.getInstance().put(Const.PARAM_SIGN_ID, signId)
        }
    }

    override fun initView() {
        signId = SPUtils.getInstance().getInt(Const.PARAM_SIGN_ID)
        if (signId == 0) {
            binding.signInBtn.text = "签到"
        } else {
            binding.signInBtn.text = "签退"
        }

        binding.signInBtn.setOnClickListener {
            if (signId == 0) {
                startSignIn()
            } else {
                endSignIn()
            }
        }

        binding.signInHeader.headerBack.setOnClickListener {
            finish()
        }

        binding.signRv.layoutManager = LinearLayoutManager(this)
        signAdapter = SignAdapter()
        binding.signRv.adapter = signAdapter

        val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, resources.getStringArray(R.array.months))
        binding.signSpinner.adapter = adapter
        binding.signSpinner.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val map = mutableMapOf<String, Int>()
                map[Const.PARAM_MONTH] = position + 1
                val json = Gson().toJson(map)
                val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                viewModel.getAllSign(body)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }

    }

    private fun startSignIn() {
        time = getTime()
        val map = mutableMapOf<String, Any>()
        map[Const.PARAM_USER_ID] = userId
        map[Const.PARAM_START_ADDRESS] = address
        map[Const.PARAM_END_ADDRESS] = address
        map[Const.PARAM_START_TIME] = time
        map[Const.PARAM_END_TIME] = time
        map[Const.PARAM_SIGN_ID] = signId

        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        viewModel.signIn(body)
    }

    private fun endSignIn() {
        time = getTime()
        val map = mutableMapOf<String, Any>()
        map[Const.PARAM_USER_ID] = userId
        map[Const.PARAM_START_ADDRESS] = address
        map[Const.PARAM_END_ADDRESS] = address
        map[Const.PARAM_START_TIME] = time
        map[Const.PARAM_END_TIME] = time
        map[Const.PARAM_SIGN_ID] = signId

        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        viewModel.signOut(body)
    }
}